package utils

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"
)

var (
	payUrl       = "https://test.allinpaygd.com/apiweb/unitorder/pay"
	payCancelUrl = "https://test.allinpaygd.com/apiweb/unitorder/cancel"
	refund       = "https://test.allinpaygd.com/apiweb/unitorder/refund"
	query        = "https://test.allinpaygd.com/apiweb/unitorder/query"
)

//支付
func TongLianPay(data map[string][]string) ([]byte, error) {
	content := make(url.Values)
	content["orgid"] = []string{""}
	content["cusid"] = []string{""}
	content["appid"] = []string{""}
	content["trxamt"] = []string{""}
	content["reqsn"] = []string{""}
	content["paytype"] = []string{""}
	content["randomstr"] = []string{""}

	str, err := json.Marshal(content)
	if err != nil {
		return nil, err
	}

	//对数据签名
	secretStr := RSA_Encrypt(str, "")

	err = json.Unmarshal(secretStr, &content)
	if err != nil {
		return nil, err
	}

	rsp, err := http.PostForm(payUrl, content)
	if err != nil {
		return nil, err
	}

	defer rsp.Body.Close()

	result, err := ioutil.ReadAll(rsp.Body)

	//验证数据
	


	return result, nil
}

//交易撤销
func PayCancel(data map[string][]string) ([]byte, error) {
	content := make(url.Values)
	content["orgid"] = []string{""}
	content["cusid"] = []string{""}
	content["appid"] = []string{""}
	content["randomstr"] = []string{""}
	content["trxamt"] = []string{""}
	content["reqsn"] = []string{""}
	content["authcode"] = []string{""}

	str, err := json.Marshal(content)
	if err != nil {
		return nil, err
	}

	//对数据进行签名
	secretStr := RSA_Encrypt(str, "")

	err = json.Unmarshal(secretStr, &content)
	if err != nil {
		return nil, err
	}

	rsp, err := http.PostForm(payCancelUrl, content)
	if err != nil {
		return nil, err
	}

	defer rsp.Body.Close()

	result, _ := ioutil.ReadAll(rsp.Body)

	return result, nil
}

//交易退款
func Refund(data map[string][]string) ([]byte, error) {
	content := make(url.Values)
	content["orgid"] = []string{""}
	content["cusid"] = []string{""}
	content["appid"] = []string{""}
	content["trxamt"] = []string{""}
	content["reqsn"] = []string{""}
	content["randomstr"] = []string{""}
	content["signtype"] = []string{""}

	str, err := json.Marshal(content)
	if err != nil {
		return nil, err
	}

	//对数据进行签名
	secretStr := RSA_Decrypt(str, "")

	err = json.Unmarshal(secretStr, &content)
	if err != nil {
		return nil, err
	}

	rsp, err := http.PostForm(refund, content)
	if err != nil {
		return nil, err
	}

	defer rsp.Body.Close()

	result, _ := ioutil.ReadAll(rsp.Body)

	return result, nil
}

//交易查询
func Query(data map[string][]string) ([]byte, error) {
	content := make(url.Values)
	content["orgid"] = []string{""}
	content["cusid"] = []string{""}
	content["appid"] = []string{""}
	content["reqsn"] = []string{""}
	content["randomstr"] = []string{""}
	content["signtype"] = []string{""}

	str, err := json.Marshal(content)
	if err != nil {
		return nil, err
	}

	//对数据进行签名
	secretStr := RSA_Decrypt(str, "")

	err = json.Unmarshal(secretStr, &content)
	if err != nil {
		return nil, err
	}

	rsp, err := http.PostForm(query, content)
	if err != nil {
		return nil, err
	}

	defer rsp.Body.Close()

	result, _ := ioutil.ReadAll(rsp.Body)

	return result, nil
}
